package util.gui;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.geom.Rectangle;

public class Component
{
	private int minWidth=0,width=0,maxWidth=Integer.MAX_VALUE;
	private int minHeight=0,height=0,maxHeight=Integer.MAX_VALUE;
	protected Color backgroundColor=LookAndFeel.backgroundColor;
	
	protected boolean drawBorder=false;
	
	/**
	 * Creates a GUI component
	 */
	public Component()
	{
		width=minWidth;
		height=minHeight;
	}
	
	public void update(GameContainer gc,Graphics g,int x,int y)
	{
		Rectangle rect=preRender(gc,g,x,y);
		render(gc,g,x,y);
		postRender(gc,g,rect,x,y);
	}
	
	private Rectangle preRender(GameContainer gc,Graphics g,int x,int y)
	{
		Rectangle rect=g.getClip();
		if(rect!=null)
		{
			int x1=Math.max(x,(int)rect.getMinX());
			int y1=Math.max(y,(int)rect.getMinY());
			int x2=Math.min(x+width,(int)(rect.getMaxX())+1);
			int y2=Math.min(y+height,(int)(rect.getMaxY())+1);
			
			//System.out.println("("+x1+","+y1+")"+","+"("+x2+","+y2+")");
			
			g.clearClip();
			g.setColor(new Color(0,0,255,128));
			//g.drawRect(x1,y1,x2-x1,y2-y1);
			
			g.setClip(x1,y1,x2-x1,y2-y1);
			
			
		}
		else
		{
			g.setClip(x,y,width,height);
		}
		
		
		
		return rect;
	}
	
	/**
	 * Render, and optionally check for input, in the component.
	 * @param gc - the current game container.
	 * @param g - the current graphics.
	 * @param x - x position to render component.
	 * @param y - y position to render component.
	 */
	protected void render(GameContainer gc,Graphics g,int x,int y)
	{
		g.setColor(backgroundColor);
		g.fillRect(x,y,width,height);
	}
	
	private void postRender(GameContainer gc,Graphics g,Rectangle rect,int x,int y)
	{
		if(drawBorder)
		{
			g.setColor(LookAndFeel.borderColor);
			g.drawRect(x,y,width-1,height-1);
		}
		
		if(rect!=null)
			g.setClip(rect);
		else
			g.clearClip();
	}
	
	/**
	 * Sets the width of the component, taking in account max and min sizes.
	 * @param width - width to set the component to.
	 */
	public void setWidth(int width)
	{
		this.width=Math.min(Math.max(minWidth,width),maxWidth);
	}
	
	/**
	 * Sets the height of the component, taking in account max and min sizes.
	 * @param height - height to set the component to.
	 */
	public void setHeight(int height)
	{
		this.height=Math.min(Math.max(minHeight,height),maxHeight);
	}
	
	/**
	 * Sets the maximum width of the component
	 * @param width - the maximum width.
	 */
	public void setMaxWidth(int maxWidth)
	{
		this.maxWidth=maxWidth;
		if(width>maxWidth)
			width=maxWidth;
	}
	
	/**
	 * Sets the maximum height of the component.
	 * @param height - the maximum height.
	 */
	public void setMaxHeight(int maxHeight)
	{
		this.maxHeight=maxHeight;
		if(height>maxHeight)
			height=maxHeight;
	}
	
	/**
	 * Sets the minimum width of the component.
	 * @param minWidth - the minimum width.
	 */
	public void setMinWidth(int minWidth)
	{
		this.minWidth=minWidth;
		if(width<minWidth)
			width=minWidth;
	}
	
	/**
	 * Sets the minimum height of the component.
	 * @param minHeight - the minimum height.
	 */
	public void setMinHeight(int minHeight)
	{
		this.minHeight=minHeight;
		if(height<minHeight)
			height=minHeight;
	}
	
	public int getWidth(){return width;}
	public int getHeight(){return height;}
	public int getMinWidth(){return minWidth;}
	public int getMinHeight(){return minHeight;}
	public int getMaxWidth(){return maxWidth;}
	public int getMaxHeight(){return maxHeight;}
	
	/**
	 * Checks whether or not the mouse is above the component.
	 * @param i - current input.
	 * @param x - x position of component.
	 * @param y - y position of component.
	 * @return whether or not the mouse is above this component
	 */
	protected boolean mouseCheck(Graphics g,Input i,int x,int y)
	{
		if(i.getMouseX()>x && i.getMouseX()<x+width && i.getMouseY()>y && i.getMouseY()<y+height)
		{
			if(g.getClip()!=null)
			{
				g.fill(g.getClip());
				if(g.getClip().contains(i.getMouseX(),i.getMouseY()))
					return true;
			}
			else
				return true;
		}
		return false;
	}
}